package org.bozo.im.service.protocols.xmpp.stanza.task.roster;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.generator.StanzaGenerator;
import org.bozo.im.service.protocols.xmpp.stanza.task.Task;
import org.bozo.im.service.protocols.xmpp.stanza.task.jabber.client.PresenceTask;
import org.bozo.im.service.protocols.xmpp.util.namespace.Namespace;

import android.util.Log;

public class RosterTask extends Task {

	public RosterTask(String pId) {
		super(pId);
	}

	@Override
	public void doTask(XMPPSession pSession, StringBuilder pBuilder) throws Exception {
		
		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.task.roster.RosterTask.doTask ---------- Retrieving the roster");
		// ---------------------------------------------------------------------------------------
		
		StanzaGenerator lRosterStanza = new StanzaGenerator(Stanza.ROSTER_QUERY);
		lRosterStanza.setDefaultNamespace(Namespace.ROSTER);
		
		StanzaGenerator lIQStanza = new StanzaGenerator(Stanza.JABBER_CLIENT_IQ);
		lIQStanza.setAttribute(Namespace.JABBER_CLIENT, "from", pSession.getConfiguration().getLoginJID());
		lIQStanza.setAttribute(Namespace.JABBER_CLIENT, "type", "get");
		lIQStanza.setAttribute(Namespace.JABBER_CLIENT, "id", getId());
		lIQStanza.setDefaultNamespace(Namespace.JABBER_CLIENT);
		lIQStanza.addChild(lRosterStanza);
		
		lIQStanza.toXml(pBuilder);
		
		// Let's retrieve the roster once we are logged in
		pSession.getTaskProcessor().pushTask(new PresenceTask(pSession.getId()), getId());
	}

}
